package com.example.myapp.ADO;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import com.example.myapp.Activity.ArticleBody;
import com.example.myapp.R;
import com.example.myapp.Model.Article;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class WX_Posts_Adapter extends RecyclerView.Adapter<WX_Posts_Adapter.ViewHolder>  {

    private LayoutInflater mInflater;
    private List<Article> postsList;
    private Context context;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public WX_Posts_Adapter(Context context, List<Article> postsList) {
        this.postsList=postsList;
        this.context=context;
        this. mInflater = LayoutInflater.from(context);//初始化
    }
    static class ViewHolder extends RecyclerView.ViewHolder{

        TextView postdetails_content;
        TextView update_btn;
        TextView day;
        TextView year;
        TextView month;
        TextView oldday;
        TextView oldmonth;
        TextView today;
        TextView twoday;
        LinearLayout postsbox;
        LinearLayout latetwoday;
        LinearLayout oneimg;
        LinearLayout yearbox;
        ImageView oneimg1;
        LinearLayout twoimg;
        ImageView twoimg1;
        ImageView twoimg2;
        LinearLayout fourimg;
        ImageView fourimg1;
        ImageView fourimg2;
        ImageView fourimg3;
        ImageView fourimg4;
        public ViewHolder (View view)
        {
            super(view);

            year=view.findViewById(R.id.year);
            oldday=view.findViewById(R.id.oldday);
            oldmonth=view.findViewById(R.id.oldmonth);
            today=view.findViewById(R.id.today);
            postsbox=view.findViewById(R.id.postsbox);
            postdetails_content=view.findViewById(R.id.postdetails_content);
            latetwoday=view.findViewById(R.id.latetwoday);
            twoday=view.findViewById(R.id.twoday);
            day=view.findViewById(R.id.day);
            month=view.findViewById(R.id.month);
            oneimg=view.findViewById(R.id.oneimg);
            oneimg1=view.findViewById(R.id.oneimg1);

            twoimg=view.findViewById(R.id.twoimg);
            twoimg1=view.findViewById(R.id.twoimg1);
            twoimg2=view.findViewById(R.id.twoimg2);
            fourimg=view.findViewById(R.id.fourimg);
            fourimg1=view.findViewById(R.id.fourimg1);
            fourimg2=view.findViewById(R.id.fourimg2);
            fourimg3=view.findViewById(R.id.fourimg3);
            fourimg4=view.findViewById(R.id.fourimg4);
            yearbox=view.findViewById(R.id.yearbox);
        }

    }



    @Override

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_weixin_posts,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int i){
        //如果没用图片
        if(postsList.get(i).getImages()==null){
            holder. oneimg.setVisibility(View.GONE);
            holder. fourimg.setVisibility(View.GONE);
        }
        //只有一张图片
        else if(postsList.get(i).getImages2()==null&&postsList.get(i).getImages3()==null){
            holder. oneimg.setVisibility(View.VISIBLE);
            holder. fourimg.setVisibility(View.GONE);
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.oneimg1);
        }else if (postsList.get(i).getImages2()!=null&&postsList.get(i).getImages4()==null){
        //   2张或三张图片
            holder. fourimg.setVisibility(View.GONE);
            holder. twoimg.setVisibility(View.VISIBLE);
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.twoimg1);
            Glide.with(context).load(postsList.get(i).getImages2().getUrl()).into(holder.twoimg2);
        }else {
            Glide.with(context).load(postsList.get(i).getImages().getUrl()).into(holder.fourimg1);
            Glide.with(context).load(postsList.get(i).getImages2().getUrl()).into(holder.fourimg2);
            Glide.with(context).load(postsList.get(i).getImages3().getUrl()).into(holder.fourimg3);
            Glide.with(context).load(postsList.get(i).getImages4().getUrl()).into(holder.fourimg4);
        }

        try {
            Date date = simpleDateFormat.parse(postsList.get(i).getCreatedAt());
               String datestr= getTime(date);
               if(datestr.equals("昨天")||datestr.equals("前天")){
                   holder.twoday.setText(datestr);
                   holder.twoday.setVisibility(View.VISIBLE);
               }else if (datestr.contains(":")){
                   holder.today.setVisibility(View.VISIBLE);
                   holder. today.setText(getTime(date));
               }else {
                   String[] S= datestr.split("-");
                   if(S.length==2){
                       holder. latetwoday.setVisibility(View.VISIBLE);
                       holder. day.setText(S[1]);
                       holder. month.setText(S[0]+"月");
                   }else {
                       holder.yearbox.setVisibility(View.VISIBLE);
                       holder.year.setText(S[0]);
                       holder. oldday.setText(S[2]);
                       holder. oldmonth.setText(S[1]);
                   }

               }


        }catch (Exception e){

        }


        holder. postdetails_content.setText(postsList.get(i).getContent());

        holder.postsbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ArticleBody.class);
                intent.putExtra("postid", postsList.get(i).getObjectId());
                context.startActivity(intent);
            }
        });//点击文章事件
        holder. postdetails_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ArticleBody.class);
                intent.putExtra("ArticleID", postsList.get(i).getObjectId());
                context.startActivity(intent);
            }
        });//点击文章事件


    }






    // 将传入时间与当前时间进行对比，是否今天\昨天\前天\同一年
    private static String getTime(Date date) {
        boolean sameYear = false;
        String todySDF = "HH:mm";
        String yesterDaySDF = "昨天";
        String beforYesterDaySDF = "前天";
        String otherSDF = "MM-dd";
        String otherYearSDF = "yyyy-MM-dd";
        SimpleDateFormat sfd = null;
        String time = "";
        Calendar dateCalendar = Calendar.getInstance();
        dateCalendar.setTime(date);
        Date now = new Date();
        Calendar todayCalendar = Calendar.getInstance();
        todayCalendar.setTime(now);
        todayCalendar.set(Calendar.HOUR_OF_DAY, 0);
        todayCalendar.set(Calendar.MINUTE, 0);

        if (dateCalendar.get(Calendar.YEAR) == todayCalendar.get(Calendar.YEAR)) {
            sameYear = true;
        } else {
            sameYear = false;
        }

        if (dateCalendar.after(todayCalendar)) {// 判断是不是今天
            sfd = new SimpleDateFormat(todySDF);
            time = sfd.format(date);
            return time;
        } else {
            todayCalendar.add(Calendar.DATE, -1);
            if (dateCalendar.after(todayCalendar)) {// 判断是不是昨天
                // sfd = new SimpleDateFormat(yesterDaySDF);
                // time = sfd.format(date);
                time = yesterDaySDF;
                return time;
            }
            todayCalendar.add(Calendar.DATE, -2);
            if (dateCalendar.after(todayCalendar)) {// 判断是不是前天
                // sfd = new SimpleDateFormat(beforYesterDaySDF);
                // time = sfd.format(date);
                time = beforYesterDaySDF;
                return time;
            }
        }

        if (sameYear) {
            sfd = new SimpleDateFormat(otherSDF);
            time = sfd.format(date);
        } else {
            sfd = new SimpleDateFormat(otherYearSDF);
            time = sfd.format(date);
        }

        return time;
    }




    @Override
    public int getItemCount(){
        return postsList.size();
    }


}
